package com.yy.yyui.combobox
{
	import com.maqie.view.UM;
	import com.yy.yyui.text.TextLabel;
	import com.util.tools.EmptyFunction;
	
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormatAlign;

	public class RunPageCombox extends Sprite
	{
		private var upBut:SimpleButton;
		private var downBut:SimpleButton;
		private var _defaultNum:int;
		private var _maxNum:int;
		private var texts:TextLabel;
		private var _changeFunc:Function;
		public function RunPageCombox(defaultNum:int,maxNum:int,upBtn:SimpleButton,downBtn:SimpleButton,w:Number,h:Number = 20,textColor:uint = 0xffffff,changeFunc:Function = null,bg:DisplayObject = null)
		{
			super();
			this.changeFunc = changeFunc;
			this.addEventListener(Event.REMOVED_FROM_STAGE,udRemoveEvent);
			var dobj:DisplayObject;
			this.upBut = upBtn;
			this.downBut = downBtn;
			dobj = this.addChild(upBut);
			dobj = this.addChild(downBut);
			dobj.x = w - this.downBut.width;
			
			texts = new TextLabel("",w-this.downBut.width*2-2);
			texts.textFormat.align = TextFormatAlign.CENTER;
			texts.textFormat.color = textColor;
			dobj = this.addChild(texts);
			dobj.x = this.upBut.x+this.upBut.width+1;
			dobj.y = (this.upBut.height - texts.height)/2+2;
			
			downBut.addEventListener(MouseEvent.CLICK,udDownEvent);
			upBut.addEventListener(MouseEvent.CLICK,udUpEvent);
			this.defaultNum = defaultNum;
			this.maxNum = maxNum;
		}
		private function udUpEvent(e:MouseEvent):void
		{
			if(defaultNum > 1)
			{
				defaultNum--;
			}
		}
		private function udDownEvent(e:MouseEvent):void
		{
			if(defaultNum < maxNum)
			{
				defaultNum++;
			}
		}
		
		private function udRemoveEvent(e:Event):void
		{
			upBut.removeEventListener(MouseEvent.CLICK,udUpEvent);
			downBut.removeEventListener(MouseEvent.CLICK,udUpEvent);
			this.changeFunc = null;
		}
		public function set maxNum(param:int):void
		{
			this._maxNum = param;
			this.update();
		}
		public function get maxNum():int
		{
			return this._maxNum;
		}
		public function set defaultNum(param:int):void
		{
			this._defaultNum = param;
				this.update();
		}
		public function get defaultNum():int
		{
			return this._defaultNum;
		}
		public function set changeFunc(param:Function):void
		{
			this._changeFunc = param;
		}
		public function get changeFunc():Function
		{
			return this._changeFunc;
		}
		private function update():void
		{
			texts.text = defaultNum+"/"+maxNum;
			if(this.changeFunc != null)
			{
				this.changeFunc(this.defaultNum);
			}
		}
		
	}
}